#include "frame_event.h"

using namespace std;
#pragma warning(disable : 4996)



vector<CFrameEvent*> CFrameEvent::vFrameThread;

void CFrameEvent::AddInstance(CFrameEvent* f)
{
	vFrameThread.push_back(f);
}

void CFrameEvent::RemoveInstance(CFrameEvent* f)
{
	vector<CFrameEvent*>::iterator it = std::find(vFrameThread.begin(), vFrameThread.end(), f);


	if (it != vFrameThread.end())
	{
		vFrameThread.erase(it);
	}
	else
	{
		pantheios::log_ERROR("[RemoveInstance] Not Found");
		
	}

	
}


CFrameEvent::CFrameEvent(CStrategySys* pStrategy_sys)
{
	m_bInvalid = false;

	//td part
	if (false == InitThreadTd())
	{
		printf("[CFrameEvent] InitThreadTd failed\n");
		return;
	}

	//mfc part
	if (false == InitThreadMFC())
	{
		printf("[CFrameEvent] InitThreadMFC failed\n");
		return;
	}

	////strategycom part
	//if (false == InitThreadStrategyCom(m_strategy_sys->Sys_GetStrategyCom()))
	//{
	//	printf("[CFrameEvent] InitThreadStrategyCom failed\n");
	//	return;
	//}



	//timer part
	if (false == InitThreadTimer())
	{
		printf("[CFrameEvent] InitThreadTimer failed\n");
		return;
	}

	//terminate part
	if (false == InitThreadTerminate())
	{
		printf("[CFrameEvent] InitThreadTerminate failed\n");
		return;
	}


	m_strategy_sys = pStrategy_sys;

	connect(this, SIGNAL(SignalQtTest()),
		this, SLOT(localslot()));

	AddInstance(this);
}


CFrameEvent::~CFrameEvent()
{
	if (false == m_vbarinfo.empty())
	{
		pantheios::log_ERROR("[~CFrameEvent] m_vbarinfo not NULL");
	}


	if (false == DestroyThreadTd())
	{
		printf("[CFrameEvent] DestroyThreadTd failed\n");
		return;
	}

	if (false == DestroyThreadMFC())
	{
		printf("[CFrameEvent] DestroyThreadMFC failed\n");
		return;
	}

	if (false == DestroyThreadTimer())
	{
		printf("[CFrameEvent] DestroyThreadTimer failed\n");
		return;
	}

	if (false == DestroyThreadTerminate())
	{
		printf("[CFrameEvent] DestroyThreadTerminate failed\n");
		return;
	}

	//if (false == DestroyThreadStrategyCom())
	//{
	//	printf("[CFrameEvent] DestroyThreadStrategyCom failed\n");
	//	return;
	//}

	if (m_strategy_sys)
	{
		delete m_strategy_sys;
		m_strategy_sys = NULL;
	}



	RemoveInstance(this);
};

bool CFrameEvent::InitBarStratety(BarInfo* pbar, char* puserdata)
{
	char* inst = pbar->instru;
	if (false == m_strategy_sys->Sys_Init(pbar, puserdata))
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[InitBarStratety] Sys_Init (%s)failed\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;
	}


	return true;
}



bool CFrameEvent::InitBarHandle(BarInfo* pbar, HANDLE* phan)
{

	m_vthreadhan.push_back(*phan);
	m_vthreadhanname.push_back(pbar->instru);

	return true;
}


//1. Only support subscribed instrument's Memory Data read now
//2. For not subscribed instrument user data, only support Date Base File reading.
//3. If need none subscribed instrument's Memory Data, you have to subscribe it. All the subscribed instrument would drive
//Sys_Run(), even if you need not it drive.
//4. Later version. Would support none subscribed instrument's Memory Data reading not through subscribe.
//according mapuserbar below to read Memory Data.
//Notice: the user data in mapuserbar is classified by instrument. Default rule is that mapuserbar's second value barser's insturment
//is the same to mapuserbar's key insturment. if user add instrument not the same with key. user would only get Data Base file data.
//but not Memory data.(The filter code is into Register() function below)

bool CFrameEvent::InitBarBroad(BarInfo* pbar, HANDLE *phan)
{

	//Get userbarser(mapuserbar) from strategy_sys------------------------

	map<string, vector<BarSeries*>> mapuserbar;
	m_strategy_sys->Sys_Getmapuserbar(mapuserbar);

	//-------------------------------------------------------------------



	//Search for pbar(parameter) related userbarser(vector<BarSeries*>)
	vector<BarSeries*>* pvbarser = NULL;

	char* inst = pbar->instru;	
	typedef map<string, vector<BarSeries*>>::iterator it_mapuserbar;
	it_mapuserbar it = mapuserbar.find(inst);
	if (it != mapuserbar.end())
		pvbarser = &(it->second);
	
	//-------------------------------------------------------------------


	//new pbroad for register and m_vbroad.push_back
	BroadcastOpen* pbroad =  new BroadcastOpen(pbar);
	if (NULL == pbroad)
	{
		printf("[InitBarBroad] pbroad[%s] Init failed\n", pbar->instru);
		return false;
	}
	//-------------------------------------------------------------------


	//Register with above userbarser(pvbarser) and only consider tick type register
	if (false == pbroad->Register(tick, phan, pvbarser))
	{
		printf("[InitBarBroad] Register failed\n");
		return false;
	}
	//-------------------------------------------------------------------



	/////Init m_vbroad with new pbroad
	m_vbroad.push_back(pbroad);
	//-------------------------------------------------------------------

	return true;
}

bool CFrameEvent::InitBarInfo(BarInfo* pbar)
{
	BarInfo* mpbar = new BarInfo;
	if (NULL == mpbar)
	{
		printf("[InitBarInfo] mpbar[%s] Init failed\n", pbar->instru);
		return false;
	}

	memcpy(mpbar, pbar, sizeof(BarInfo));
	m_vbarinfo.push_back(mpbar);

	return true;
}

bool CFrameEvent::InitSingaltoQt(BarInfo* pbar)
{

	//Get userbarser(mapuserbar) from strategy_sys------------------------

	map<string, vector<BarSeries*>> mapuserbar;
	m_strategy_sys->Sys_Getmapuserbar(mapuserbar);

	//-------------------------------------------------------------------



	//Search for pbar(parameter) related userbarser(vector<BarSeries*>)
	BarSeries* pbarser = NULL;

	char* inst = pbar->instru;	
	typedef map<string, vector<BarSeries*>>::iterator it_mapuserbar;
	it_mapuserbar it = mapuserbar.find(inst);
	if (it != mapuserbar.end())
	{
		vector<BarSeries*> pvbarser = (it->second);
		pbarser = pvbarser[0];
	}
	else
	{
		pantheios::log_ERROR("[InitSingaltoQt]", inst,  "Not found in mapuserbar");
		return false;
	}
		

		

	//-------------------------------------------------------------------

	pantheios::log_WARNING("[InitSingaltoQt] ", pbarser->m_pcur->inst, " index:", panint(pbarser->m_pcur->index), " m_index:", panint(pbarser->m_index), " tickcnt:", panint(pbarser->m_pcur->tickcnt));

	BarData* pData = new BarData;
	if (pData)
	{
		memcpy(pData, pbarser->m_pcur, sizeof(BarData));

		emit SignalInit(pbarser, pData);
	}


	

	return true;
}




bool CFrameEvent::InitBar(BarInfo* pbar, char* puserdata)
{

	if (false == InitBarStratety(pbar, puserdata))
	{
		printf("[InitBar] InitBarStratety[%s] failed\n", pbar->instru);
		return false;
	}

	HANDLE mdhan;
	if (false == InitBarBroad(pbar, &mdhan))
	{
		printf("[InitBar] InitBarBroad[%s] failed\n", pbar->instru);
		return false;
	}
	if (false == InitBarHandle(pbar, &mdhan))
	{
		printf("[InitBar] InitBarHandle[%s] failed\n", pbar->instru);
		return false;
	}
	if (false == InitBarInfo(pbar))
	{
		printf("[InitBar] InitBarInfo[%s] failed\n", pbar->instru);
		return false;
	}


	if (false == InitSingaltoQt(pbar))
	{
		printf("[InitBar] InitSingaltoQt[%s] failed\n", pbar->instru);
		return false;
	}


	
	return true;
}

bool CFrameEvent::DestroyBarStrategy(BarInfo* pbar)
{
	char* inst = pbar->instru;
	if (false == m_strategy_sys->Sys_Destroy(pbar))
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[DestroyStrategy] Sys_Destroy (%s)failed\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;
	}


	return true;
}




bool CFrameEvent::DestroyBarHandle(BarInfo* pbar)
{
	vector<string>::size_type size = m_vthreadhanname.size();
	unsigned int index = 0;
	for (index = 0; index < size; index++)
	{
		const char* inst = m_vthreadhanname[index].c_str();
		if (0 == strcmp(inst, pbar->instru))
		{
			break;
		}
	}
	if (index == size)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[DestroyBarHandle] not found:%s\n", pbar->instru);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  	
		return false;
	}



	vector<string>::iterator it = m_vthreadhanname.begin() + index;
	m_vthreadhanname.erase(it);

	vector<HANDLE>::iterator it2 = m_vthreadhan.begin() + index;
	m_vthreadhan.erase(it2);

	
	return true;
}

bool CFrameEvent::DestroyBarBroad(BarInfo* pbar)
{
	vector<BroadcastOpen*>::size_type barcnt = m_vbroad.size();
	unsigned int bindex = 0;
	for (bindex = 0; bindex < barcnt; bindex++)
	{
		char* inst = m_vbroad[bindex]->m_barinfo.instru;
		if (0 == strcmp(inst, pbar->instru))
		{
			break;
		}
	}
	if (bindex == barcnt)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[DestroyBarBroad] not found:%s\n", pbar->instru);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  	
		return false;
	}



	vector<BroadcastOpen*>::iterator it = m_vbroad.begin() + bindex;
	BroadcastOpen* pbroad = *it;
	if (pbroad)
	{
		//if (TimerFire_namespace::invalid == *ptimertype)
			pbroad->UnRegister(tick);

		delete pbroad;
	}
	m_vbroad.erase(it);

	return true;
}

bool CFrameEvent::DestroyBarInfo(BarInfo* pbar)
{
	vector<BarInfo*>::size_type barcnt = m_vbarinfo.size();
	unsigned int bindex = 0;
	for (bindex = 0; bindex < barcnt; bindex++)
	{
		char* inst = m_vbarinfo[bindex]->instru;
		if (0 == strcmp(inst, pbar->instru))
		{
			break;
		}
	}
	if (bindex == barcnt)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[DestroyBarInfo] not found:%s\n", pbar->instru);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  	
		return false;
	}



	vector<BarInfo*>::iterator it = m_vbarinfo.begin() + bindex;
	BarInfo* pbarinfo = *it;
	if (pbarinfo)
		delete pbarinfo;

	m_vbarinfo.erase(it);

	return true;
}

bool CFrameEvent::DestroySingaltoQt(BarInfo* pbar)
{

	pantheios::log_WARNING("[DestroySingaltoQt] ", pbar->instru);

	emit SignalDestroy(pbar);

	return true;
}


bool CFrameEvent::DestroyBar(BarInfo* pbar)
{
	if (false == DestroySingaltoQt(pbar))
	{
		printf("[DestroyBar] DestroySingaltoQt[%s] failed\n", pbar->instru);
		return false;
	}
	
	if (false == DestroyBarInfo(pbar))
	{
		printf("[DestroyBar] DestroyBarInfo[%s] failed\n", pbar->instru);
		return false;
	}
	if (false == DestroyBarHandle(pbar))
	{
		printf("[DestroyBar] DestroyBarSysMapser[%s] failed\n", pbar->instru);
		return false;
	}
	if (false == DestroyBarBroad(pbar))
	{
		printf("[DestroyBar] DestroyBarBroad[%s] failed\n", pbar->instru);
		return false;
	}
	if (false == DestroyBarStrategy(pbar))
	{
		printf("[DestroyBar] DestroyBarStrategy[%s] failed\n", pbar->instru);
		return false;
	}



	return true;
}

bool CFrameEvent::EditBarStrategy(BarInfo* pbar, char* puserdata)
{
	if (false == m_strategy_sys->Sys_Edit(pbar, puserdata))
	{
		printf("[EditBarStrategy] Edit[%s] failed\n", pbar->instru);
		return false;
	}

	return true;
}



bool CFrameEvent::EditBar(BarInfo* pbar, char* puserdata)
{
	if (false == EditBarStrategy(pbar, puserdata))
	{
		printf("[EditBar] EditBarStrategy[%s] failed\n", pbar->instru);
		return false;
	}

	return true;
}


//////////////////////////////////////////////////////////////////////////


bool CFrameEvent::InitThreadTimer()
{
	/**************************m_timercom init**********************************/

	m_timercom.pmq = new Mq(MAX_DEFAULT_MQSIZE * sizeof(TimerData));
	if (NULL == m_timercom.pmq)
	{
		printf("[InitThreadTimer] m_timercom.pmq Init failed\n");
		return false;
	}
	m_timercom.han[0] = CreateSemaphore(NULL, 0, MAX_DEFAULT_SEMACNT, NULL);
	if (NULL == m_timercom.han[0])
	{
		printf("[InitThreadTimer] m_timercom.han[0] Init failed\n");
		return false;
	} 
	m_timercom.han[1] = CreateSemaphore(NULL, 0, MAX_DEFAULT_SEMACNT, NULL);
	if (NULL == m_timercom.han[1])
	{
		printf("[InitThreadTimer] m_timercom.han[1] Init failed\n");
		return false;
	} 
	m_timercom.hanid = "timerhanid";

	/************************************************************************/


	/*********copy to m_vthreadhanname/m_vthreadhan****************************/

	m_vthreadhanname.push_back(m_timercom.hanid);

	m_vthreadhan.push_back(m_timercom.han[0]);
	/************************************************************************/

	return true;
}

bool CFrameEvent::DestroyThreadTimer()
{
	/*********erase from m_vthreadhanname/m_vthreadhan****************************/

	vector<string>::size_type size = m_vthreadhanname.size();
	unsigned int index = 0;
	for (index = 0; index < size; index++)
	{
		if (m_vthreadhanname[index] == m_timercom.hanid)
		{
			break;
		}
	}
	if (index == size)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[DestroyThreadTimer] not found:%s\n", m_timercom.hanid.c_str());
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  	
		return false;
	}

	vector<string>::iterator it = m_vthreadhanname.begin() + index;
	m_vthreadhanname.erase(it);

	vector<HANDLE>::iterator it2 = m_vthreadhan.begin() + index;
	m_vthreadhan.erase(it2);

	/************************************************************************/


	/**************************m_mfccom erase**********************************/

	if (m_timercom.pmq)
	{
		delete m_timercom.pmq;
		m_timercom.pmq = NULL;
	}


	if (m_timercom.han[0])
	{
		CloseHandle(m_timercom.han[0]);
		m_timercom.han[0] = NULL;
	}

	if (m_timercom.han[1])
	{
		CloseHandle(m_timercom.han[1]);
		m_timercom.han[1] = NULL;
	}

	/************************************************************************/



	return true;
}


void CFrameEvent::ResetThreadTimer()
{
	m_timercom.pmq->ResetMq();
}

bool CFrameEvent::InitThreadMFC()
{
	/**************************m_tdcom init**********************************/

	m_mfccom.pmq = new Mq(MAX_DEFAULT_MQSIZE * sizeof(MFCData));
	if (NULL == m_mfccom.pmq)
	{
		printf("[InitThreadMFC] m_mfccom.pmq Init failed\n");
		return false;
	}
	m_mfccom.han[0] = CreateSemaphore(NULL, 0, MAX_DEFAULT_SEMACNT, NULL);
	if (NULL == m_mfccom.han[0])
	{
		printf("[InitThreadMFC] m_mfccom.han[0] Init failed\n");
		return false;
	} 
	m_mfccom.han[1] = CreateSemaphore(NULL, 0, MAX_DEFAULT_SEMACNT, NULL);
	if (NULL == m_mfccom.han[1])
	{
		printf("[InitThreadMFC] m_mfccom.han[1] Init failed\n");
		return false;
	} 
	m_mfccom.hanid = "mfchanid";

	/************************************************************************/


	/*********copy to m_vthreadhanname/m_vthreadhan****************************/

	m_vthreadhanname.push_back(m_mfccom.hanid);

	m_vthreadhan.push_back(m_mfccom.han[0]);
	/************************************************************************/

	return true;
}

bool CFrameEvent::DestroyThreadMFC()
{
	/*********erase from m_vthreadhanname/m_vthreadhan****************************/

	vector<string>::size_type size = m_vthreadhanname.size();
	unsigned int index = 0;
	for (index = 0; index < size; index++)
	{
		if (m_vthreadhanname[index] == m_mfccom.hanid)
		{
			break;
		}
	}
	if (index == size)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[DestroyThreadMFC] not found:%s\n", m_mfccom.hanid.c_str());
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  	
		return false;
	}

	vector<string>::iterator it = m_vthreadhanname.begin() + index;
	m_vthreadhanname.erase(it);

	vector<HANDLE>::iterator it2 = m_vthreadhan.begin() + index;
	m_vthreadhan.erase(it2);

	/************************************************************************/


	/**************************m_mfccom erase**********************************/

	if (m_mfccom.pmq)
	{
		delete m_mfccom.pmq;
		m_mfccom.pmq = NULL;
	}


	if (m_mfccom.han[0])
	{
		CloseHandle(m_mfccom.han[0]);
		m_mfccom.han[0] = NULL;
	}

	if (m_mfccom.han[1])
	{
		CloseHandle(m_mfccom.han[1]);
		m_mfccom.han[1] = NULL;
	}

	/************************************************************************/



	return true;
}

void CFrameEvent::ResetThreadMFC()
{
	m_mfccom.pmq->ResetMq();
}

bool CFrameEvent::InitThreadTd()
{
	/**************************m_tdcom init**********************************/

	m_tdcom.pmq = new Mq(MAX_TDMQ_SIZE);
	if (NULL == m_tdcom.pmq)
	{
		printf("[InitThreadTd] m_tdcom.pmq Init failed\n");
		return false;
	}
	m_tdcom.han = CreateSemaphore(NULL, 0, MAX_DEFAULT_SEMACNT, NULL);
	if (NULL == m_tdcom.han)
	{
		printf("[InitThreadTd] m_tdcom.han Init failed\n");
		return false;
	} 
	m_tdcom.hanid = "tradehanid";

	/************************************************************************/


	/*********copy to m_vthreadhanname/m_vthreadhan****************************/

	m_vthreadhanname.push_back(m_tdcom.hanid);

	m_vthreadhan.push_back(m_tdcom.han);
	/************************************************************************/
	return true;
}

bool CFrameEvent::DestroyThreadTd()
{
	/*********erase from m_vthreadhanname/m_vthreadhan****************************/

	vector<string>::size_type size = m_vthreadhanname.size();
	unsigned int index = 0;
	for (index = 0; index < size; index++)
	{
		if (m_vthreadhanname[index] == m_tdcom.hanid)
		{
			break;
		}
	}
	if (index == size)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[DestroyThreadTd] not found:%s\n", m_tdcom.hanid.c_str());
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  	
		return false;
	}

	vector<string>::iterator it = m_vthreadhanname.begin() + index;
	m_vthreadhanname.erase(it);

	vector<HANDLE>::iterator it2 = m_vthreadhan.begin() + index;
	m_vthreadhan.erase(it2);

	/************************************************************************/


	/**************************m_tdcom erase**********************************/

	if (m_tdcom.pmq)
	{
		delete m_tdcom.pmq;
		m_tdcom.pmq = NULL;
	}


	if (m_tdcom.han)
	{
		CloseHandle(m_tdcom.han);
		m_tdcom.han = NULL;
	}

	/************************************************************************/

	return true;
}

void CFrameEvent::ResetThreadTd()
{
	m_tdcom.pmq->ResetMq();
}


bool CFrameEvent::InitThreadTerminate()
{
	/**************************m_tdcom init**********************************/

	m_terminatecom.han = CreateSemaphore(NULL, 0, MAX_DEFAULT_SEMACNT, NULL);
	if (NULL == m_terminatecom.han)
	{
		printf("[InitThreadTerminate] m_tdcom.han Init failed\n");
		return false;
	} 
	m_terminatecom.hanid = "terminatehanid";

	/************************************************************************/


	/*********copy to m_vthreadhanname/m_vthreadhan****************************/

	m_vthreadhanname.push_back(m_terminatecom.hanid);

	m_vthreadhan.push_back(m_terminatecom.han);
	/************************************************************************/
	return true;
}

bool CFrameEvent::DestroyThreadTerminate()
{
	/*********erase from m_vthreadhanname/m_vthreadhan****************************/

	vector<string>::size_type size = m_vthreadhanname.size();
	unsigned int index = 0;
	for (index = 0; index < size; index++)
	{
		if (m_vthreadhanname[index] == m_terminatecom.hanid)
		{
			break;
		}
	}
	if (index == size)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[DestroyThreadTerminate] not found:%s\n", m_terminatecom.hanid.c_str());
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  	
		return false;
	}

	vector<string>::iterator it = m_vthreadhanname.begin() + index;
	m_vthreadhanname.erase(it);

	vector<HANDLE>::iterator it2 = m_vthreadhan.begin() + index;
	m_vthreadhan.erase(it2);

	/************************************************************************/


	/**************************m_terminatecom erase**********************************/

	
	if (m_terminatecom.han)
	{
		CloseHandle(m_terminatecom.han);
		m_terminatecom.han = NULL;
	}

	/************************************************************************/

	return true;
}


//
//bool CFrameEvent::InitThreadStrategyCom(ThreadCom_Round& strategycom)
//{
//	/**************************m_strategycom init**********************************/
//
//	m_strategycom.pmq = strategycom.pmq;
//
//	m_strategycom.han[0] = strategycom.han[0];
//
//	m_strategycom.han[1] = strategycom.han[1];
//
//	m_strategycom.hanid = strategycom.hanid;
//
//	/************************************************************************/
//
//
//	/*********copy to m_vthreadhanname/m_vthreadhan****************************/
//
//	m_vthreadhanname.push_back(m_strategycom.hanid);
//
//	m_vthreadhan.push_back(m_strategycom.han[0]);
//	/************************************************************************/
//
//	return true;
//}
//
//bool CFrameEvent::DestroyThreadStrategyCom()
//{
//	/*********erase from m_vthreadhanname/m_vthreadhan****************************/
//
//	vector<string>::size_type size = m_vthreadhanname.size();
//	unsigned int index = 0;
//	for (index = 0; index < size; index++)
//	{
//		if (m_vthreadhanname[index] == m_strategycom.hanid)
//		{
//			break;
//		}
//	}
//	if (index == size)
//	{
//		SetConsoleColor(FOREGROUND_GREEN);  
//		printf("[DestroyThreadStrategyCom] not found:%s\n", m_strategycom.hanid.c_str());
//		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  	
//		return false;
//	}
//
//	vector<string>::iterator it = m_vthreadhanname.begin() + index;
//	m_vthreadhanname.erase(it);
//
//	vector<HANDLE>::iterator it2 = m_vthreadhan.begin() + index;
//	m_vthreadhan.erase(it2);
//
//	/************************************************************************/
//
//
//
//	return true;
//}


void CFrameEvent::Frame()
{
	DWORD dwSem;
	unsigned int size = m_vthreadhan.size();

	dwSem = WaitForMultipleObjects(size, &m_vthreadhan[0], false, INFINITE);

	dwSem -= WAIT_OBJECT_0;

	if (dwSem == 0)
	{
		Td_Frame();
	} 
	else if (dwSem == 1)
	{
		MFC_Frame();
	} 
	else if (dwSem == 2)
	{
		Timer_Frame();	
	} 
	else if (dwSem == 3)
	{
		m_bInvalid = true;
		pantheios::log_NOTICE("[Frame] terminate sema");
	} 
	else if (dwSem <= size)
	{
		Md_Frame(dwSem - 4);
	}
	else
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Frame] Wait error: %d\n", GetLastError()); 
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	}


}


void CFrameEvent::ReleaseTimerSemAndSetData(TimerData* pdata)
{
	m_timercom.pmq->SetData((char*) pdata, sizeof(TimerData));

	
	long precnt = 0;
	ReleaseSemaphore(m_timercom.han[0], 1, &precnt);
	if (0 != precnt)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[ReleaseTimerSemAndSetData] previous semaphore cnt is: "<<precnt<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	}


	WaitForSingleObject(m_timercom.han[1], INFINITE);

	//m_strategy_sys->m_str

	pantheios::log_NOTICE("[ReleaseTimerSemAndSetData] after wait sema round");

}

void CFrameEvent::ReleaseMFCSemAndSetData(MFCData* pmfcdata)
{
	m_mfccom.pmq->SetData((char*) pmfcdata, sizeof(MFCData));

	if (0 != pmfcdata->varsize && NULL != pmfcdata->pvardata)
	{
		m_mfccom.pmq->SetData(pmfcdata->pvardata, pmfcdata->varsize);
	}

	
	CTimeCounter tc;
	tc.Start();


	long precnt = 0;
	ReleaseSemaphore(m_mfccom.han[0], 1, &precnt);
	//if (0 != precnt)
	//{
	//	SetConsoleColor(FOREGROUND_GREEN);  
	//	cerr<<"[ReleaseMFCSemAndSetData] previous semaphore cnt is: "<<precnt<<endl;
	//	SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	//}


	//WaitForSingleObject(m_mfccom.han[1], INFINITE);

	char* strtype = NULL;
	if (pmfcdata->type == MFC_namespace::add)
		strtype = "add";
	else if (pmfcdata->type == MFC_namespace::del)
		strtype = "del";
	else if (pmfcdata->type == MFC_namespace::edit)
		strtype = "edit";
	else if (pmfcdata->type == MFC_namespace::strategycom)
		strtype = "strategycom";

	pantheios::log_NOTICE("[ReleaseMFCSemAndSetData] ", pmfcdata->bar.instru, ", ", strtype, ", elapse:", panreal(tc.Elapse()));

}

void CFrameEvent::ReleaseTdSemAndSetData(TdData* pdata)
{
	/******************Copy to TD MQ*********************************************/

	m_tdcom.pmq->SetData((char*)(pdata), sizeof(TdData));


	if (0 != pdata->varsize && NULL != pdata->pvardata)
	{
		m_tdcom.pmq->SetData(pdata->pvardata, pdata->varsize);
	}


	//if (type == order) // sizeof(CThostFtdcOrderField) is 576
	//{
	//	//unsigned int size = sizeof(CThostFtdcOrderField);	//576
	//	m_tdcom.pmq->SetData(pdata, 576);
	//} 
	//else if (type == trade) // sizeof(CThostFtdcTradeField) is 576
	//{
	//	//unsigned int size = sizeof(CThostFtdcTradeField);	//320
	//	m_tdcom.pmq->SetData(pdata, 320);
	//} 
	//else if (type == cancelorderfailed) // sizeof(CThostFtdcInputOrderActionField) is 152
	//{
	//	//unsigned int size = sizeof(CThostFtdcInputOrderActionField);	//152
	//	m_tdcom.pmq->SetData(pdata, 152);
	//} 

	
	/************************************************************************/

	/******************Release semaphore**************************************/
	long precnt = 0;
	ReleaseSemaphore(m_tdcom.han, 1, &precnt);
	if (0 != precnt)
	{
		/*SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[ReleaseTdSemAndSetData] previous semaphore cnt is: "<<precnt<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  */
	}
	/************************************************************************/
}

void CFrameEvent::ReleaseTerminateSem()
{
	long precnt = 0;
	//printf("[TimerFire] b4 Release\n");
	ReleaseSemaphore(m_terminatecom.han, 1, &precnt);
	//printf("[TimerFire] after Release\n");
	if (0 != precnt)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[CFrameDB::ReleaseTerminateSem] Terminate delay previous semaphore cnt is: "<<precnt<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	}
}

//void CFrameEvent::ReleaseStrategycomSemAndSetData(StrategyComData* pdata)
//{
//	if (m_strategycom.pmq && m_strategycom.han[0] && m_strategycom.han[1])
//	{
//		if (0 != pdata->varsize && NULL != pdata->pvardata)
//		{
//			m_strategycom.pmq->SetData(pdata->pvardata, pdata->varsize);
//		}
//
//		long precnt = 0;
//		ReleaseSemaphore(m_strategycom.han[0], 1, &precnt);
//		//if (0 != precnt)
//		//{
//		//	SetConsoleColor(FOREGROUND_GREEN);  
//		//	cerr<<"[ReleaseTimerSemAndSetData] previous semaphore cnt is: "<<precnt<<endl;
//		//	SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
//		//}
//
//		WaitForSingleObject(m_strategycom.han[1], INFINITE);
//
//
//		pantheios::log_NOTICE("[ReleaseStrategycomSemAndSetData] after wait m_strategycom.han[1] round");
//	}
//	else
//	{
//		pantheios::log_WARNING("[ReleaseStrategycomSemAndSetData] invalid m_strategycom");
//	}
//
//	
//}

void CFrameEvent::ReleaseFrameSemaAndSetData(FrameCom_Data* pFramecomData)
{
	FrameCom_Type type = pFramecomData->type;

	unsigned int varsize = pFramecomData->varsize;

	char* pvardata = NULL;
	if (0 != varsize)
	{
		pvardata = pFramecomData->pvardata;
	}


	if (type == FrameCom_Type::tdtype)
	{
		ReleaseTdSemAndSetData((TdData*) pvardata);
	} 
	else if (type == FrameCom_Type::timerfiretype)
	{
		ReleaseTimerSemAndSetData((TimerData*) pvardata);
	}
	else if (type == FrameCom_Type::mfctype)
	{
		ReleaseMFCSemAndSetData((MFCData*) pvardata);
	}
	else if (type == FrameCom_Type::terminatetype)
	{
		ReleaseTerminateSem();
	}
	//else if (type == FrameCom_Type::strategycom)
	//{
	//	ReleaseStrategycomSemAndSetData((StrategyComData*) pvardata);
	//}



}



void CFrameEvent::Timer_Frame()
{
	TimerData *pdata = (TimerData*)(m_timercom.pmq->GetData(sizeof(TimerData)));


	TimerFire_namespace::TimerFireType type = pdata->type;


	/************************************************************************/


	/********************Judge type and invoke related func******************/

	if (type == TimerFire_namespace::statisfiretimer)
	{
		m_strategy_sys->Sys_OnStatistic();

	} 
	else	if (type == TimerFire_namespace::appreinitfiretimer)
	{
		ResetThreadTimer();

		ResetThreadMFC();

		ResetThreadTd();

	} 
	/************************************************************************/

	/********************Release round semaphore******************/
	long precnt = 0;
	ReleaseSemaphore(m_timercom.han[1], 1, &precnt);
	if (0 != precnt)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[Timer_Frame] previous semaphore cnt is: "<<precnt<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	}
	/************************************************************************/
}



void CFrameEvent::MFC_Frame()
{
	/********************Get Data from MFC MQ*******************************/
	MFCData *pmfcdata = (MFCData*)(m_mfccom.pmq->GetData(sizeof(MFCData)));


	MFC_namespace::MFCtype type = pmfcdata->type;

	BarInfo* pbar = &(pmfcdata->bar);

	TimerFire_namespace::TimerFireType* ptimertype = &(pmfcdata->timertype);

	unsigned int varsize = pmfcdata->varsize;


	char* pvardata = NULL;
	if (0 != varsize)
	{
		pvardata = (m_mfccom.pmq->GetData(varsize));
	}
	
	/************************************************************************/


	/********************Judge type and invoke related func******************/

	if (type == MFC_namespace::add)
	{
		pantheios::log_WARNING("[MFC_Frame] Add-->pabr:", pbar->instru, ", ptimertype:", panint(*ptimertype), ", pvardata:", pvardata);


		if (false == InitBar(pbar, pvardata))
		{
			printf("[MFC_Frame] InitBar[%s] failed\n", pbar->instru);
			return;
		}



	} 
	else if (type == MFC_namespace::del)
	{
		pantheios::log_WARNING("[MFC_Frame] Del-->pabr:", pbar->instru, ", ptimertype:", panint(*ptimertype));


		if (false == DestroyBar(pbar))
		{
			printf("[MFC_Frame] DestroyBar[%s] failed\n", pbar->instru);
			return;
		}
	}
	else if (type == MFC_namespace::edit)
	{
		if (false == EditBar(pbar, pvardata))
		{
			printf("[MFC_Frame] DestroyBar[%s] failed\n", pbar->instru);
			return;
		}
	}
	else if (type == MFC_namespace::strategycom)
	{
		m_strategy_sys->Sys_StrategyCom(varsize, pvardata);
	}
	/************************************************************************/


	/********************Release round semaphore******************/
	/*long precnt = 0;
	ReleaseSemaphore(m_mfccom.han[1], 1, &precnt);
	if (0 != precnt)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[MFC_Frame] previous semaphore cnt is: "<<precnt<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	}*/
	/************************************************************************/
}




void CFrameEvent::Td_Frame()
{
	/********************Get Data from Td MQ*******************************/
	TdData *ptddata = (TdData*)(m_tdcom.pmq->GetData(sizeof(TdData)));

	Tdtype tdtype = ptddata->type;

	unsigned int varsize = ptddata->varsize;

	char* pvardata = NULL;
	if (0 != varsize)
	{
		pvardata = (m_tdcom.pmq->GetData(varsize));
	}



	//TdData tddata = *(Tdtype*)(m_tdcom.pmq->GetData(4));
	//
	//char* pdata = NULL;
	////char* preqid = NULL;
	//if (tdtype == order) // sizeof(CThostFtdcOrderField) is 576
	//{
	//	//unsigned int size = sizeof(CThostFtdcOrderField);	//576
	//	pdata = (m_tdcom.pmq->GetData(576));
	//} 
	//else if (tdtype == trade) // sizeof(CThostFtdcTradeField) is 576
	//{
	//	//unsigned int size = sizeof(CThostFtdcTradeField);	//320
	//	pdata = (m_tdcom.pmq->GetData(320));
	//}
	//else if (tdtype == cancelorderfailed) // sizeof(CThostFtdcInputOrderActionField) is 152
	//{
	//	//unsigned int size = sizeof(CThostFtdcInputOrderActionField);	//152
	//	pdata = (m_tdcom.pmq->GetData(152));
	//} 
	////else if (tdtype == qryinst) // sizeof(CThostFtdcInputOrderActionField) is 152
	////{
	////	//first set reqid
	////	preqid = (m_tdcom.pmq->GetData(4));


	////	//second set sizeof(CThostFtdcInputOrderActionField 232
	////	//unsigned int size = sizeof(CThostFtdcInstrumentField);	//?
	////	pdata = (m_tdcom.pmq->GetData(232));
	////}

	/************************************************************************/


	/********************Judge type and invoke related func******************/
	if (order == tdtype)
	{

		CThostFtdcOrderField* porder = (CThostFtdcOrderField*) pvardata;

		if (porder->OrderStatus == THOST_FTDC_OST_Canceled)
		{
			//Td_OnCancelOrderFrame(porder);
			m_strategy_sys->Sys_OnCancelOrder(porder);
		} 
		else
		{
			//Td_OnOrderFrame(porder);
			m_strategy_sys->Sys_OnOrder(porder);
		}
	} 
	else if (trade == tdtype)
	{
		//Td_OnTradeFrame((CThostFtdcTradeField*)pdata);
		m_strategy_sys->Sys_OnTrade((CThostFtdcTradeField*)pvardata);
	}
	else if (cancelorderfailed == tdtype)
	{
		//Td_OnCancelOrderFailedFrame((CThostFtdcInputOrderActionField*)pdata);
		m_strategy_sys->Sys_OnCancelOrderFailed((CThostFtdcInputOrderActionField*)pvardata);
	}
	//else if (qryinst == tdtype)
	//{
	//	//Td_OnQryInstrumentFrame((CThostFtdcInstrumentField*) pdata, (int*) preqid);
	//	m_strategy_sys->Sys_OnQryInstrument((CThostFtdcInstrumentField*) pdata, *preqid);
	//}
	else
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Td_Frame] unmached tdtype:%d", tdtype);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  

	}

	/************************************************************************/
}





void CFrameEvent::Md_Frame(DWORD index)
{
	BarData* ptickdata = m_vbroad[index]->GetDriveBar(tick);
	
//
//*	printf("[CFrameEvent::Md_Frame] ins:%s, lastprice:%0.2f\n",
//		m_vbroad[index]->m_barinfo.instru, ptickdata->LastPric*/e);


	char* inst = (char*)(m_vbroad[index]->m_barinfo.instru);

	
	

	m_strategy_sys->Sys_Run(ptickdata, inst);

}
//
//void CFrameEvent::StrategyCom_Frame()
//{
//	/********************Get Data from MFC MQ*******************************/
//	StrategyComData *pdata = (StrategyComData*)(m_strategycom.pmq->GetData(sizeof(StrategyComData)));
//
//	unsigned varsize = pdata->varsize;
//
//
//	char* pvardata = NULL;
//	if (0 != varsize)
//	{
//		pvardata = (m_strategycom.pmq->GetData(varsize));
//	}
//
//	/************************************************************************/
//
//
//
//
//
//
//	/********************Release round semaphore******************/
//	long precnt = 0;
//	ReleaseSemaphore(m_strategycom.han[1], 1, &precnt);
//	if (0 != precnt)
//	{
//		SetConsoleColor(FOREGROUND_GREEN);  
//		cerr<<"[StrategyCom_Frame] previous semaphore cnt is: "<<precnt<<endl;
//		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
//	}
//	/************************************************************************/
//}

void CFrameEvent::OnQryInstrument(CThostFtdcInstrumentField *pInstrument, int reqid)
{
	m_strategy_sys->Sys_OnQryInstrument(pInstrument, reqid);
	
}


void CFrameEvent::OnQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, int reqid)
{
	m_strategy_sys->Sys_OnQryTradingAccount(pTradingAccount, reqid);

}


void CFrameEvent::OnQrytCommissionRate(CThostFtdcInstrumentCommissionRateField *pInstrumentCommissionRate, int reqid)
{
	m_strategy_sys->Sys_OnQrytCommissionRate(pInstrumentCommissionRate, reqid);

}



CStrategySys* CFrameEvent::StrategySys()
{
	return m_strategy_sys;
}